import type { UserInfo, ServiceChannelStore, KOCSIMStore } from "../types/index";

/**
 * 将创建过的客服通道信息存储到本地
 */

const store:KOCSIMStore = {
  /**
   * 当前聊天用户
   * {
    "uid": string,
    "authToken": string,
    "isVisitor": number
    }
   **/
  currentUser: uni.getStorageSync("__WKIM__currentUser") || null,
  /** 客服通道列表 (当前用户相同客服只保存一个客服通道)*/
  serviceChannelList: [
    // {
    //   id: "1871501793549602818",
    //   createTime: "2024-12-24 18:22:53",
    //   status: 1,
    //   isDeleted: 0,
    //   channelId: "1871501793293750274",
    //   customerId: "1871501629271298050",
    //   servicePersonUserId: "1871039447198851073",
    //   isVisitor: 1,
    //   channelStatus: "waiting",
    //   lastConversationTime: "2024-12-24 18:22:53",
    //   visitorName: "访客20241224182253",
    // },
  ],
  /** 最近使用表情 */
  historyEmojis: uni.getStorageSync("__WKIM__historyEmojis") || [],
  // 初始化
  init() {
    const LOCAL_SERVICE_CHANNEL_LIST = uni.getStorageSync("__WKIM__serviceChannelList");
    if (LOCAL_SERVICE_CHANNEL_LIST) {
      this.serviceChannelList = LOCAL_SERVICE_CHANNEL_LIST;
    }
    // 获取当前用户
    const LOCAL_CURRENT_USER = uni.getStorageSync("__WKIM__currentUser");
    if (LOCAL_CURRENT_USER) {
      this.currentUser = LOCAL_CURRENT_USER;
    }
  },
  // 保存当前聊天用户
  saveCurrentUser(user: UserInfo) {
    // belongAddr 用户所属 ws 地址
    this.currentUser = user || null;
    uni.setStorageSync("__WKIM__currentUser", this.currentUser);
  },
  // 获取当前聊天用户
  getCurrentUser() {
    return this.currentUser;
  },
  // 保存客服通道列表
  saveServiceChannelList() {
    uni.setStorageSync("__WKIM__serviceChannelList", this.serviceChannelList);
  },
  // 获取客服通道列表
  getServiceChannelList() {
    return this.serviceChannelList;
  },
  // 添加客服通道
  addServiceChannel(serviceChannel: ServiceChannelStore) {
    // 查看是否已经存在相同服务人员 servicePersonUserId 的客服通道
    const index = this.serviceChannelList.findIndex(
      (v: ServiceChannelStore) => v.servicePersonUserId === serviceChannel.servicePersonUserId
    );
    if (index === -1) {
      this.serviceChannelList.push(serviceChannel);
    } else {
      this.serviceChannelList[index] = serviceChannel;
    }
    this.saveServiceChannelList();
  },
  // 查询服务人员对应的缓存客服通道
  getServiceChannelByUserId(servicePersonUserId: string) {
    return this.serviceChannelList.find((v: ServiceChannelStore) => v.servicePersonUserId === servicePersonUserId);
  },
  // 删除客服通道
  removeServiceChannel(serviceChannel: any) {
    this.serviceChannelList = this.serviceChannelList.filter((v: ServiceChannelStore) => v.id !== serviceChannel.id);
    this.saveServiceChannelList();
  },
  // 保存最近使用表情,最大保存15个
  saveHistoryEmojis(emoji: string) {
    // 如果已经存在，则移动到最前
    const index = this.historyEmojis.findIndex((v: string) => v === emoji);
    if (index !== -1) {
      this.historyEmojis.splice(index, 1);
      this.historyEmojis.unshift(emoji);
    } else {
      this.historyEmojis.unshift(emoji);
    }
    if (this.historyEmojis.length >= 15) {
      this.historyEmojis.shift();
    }
    uni.setStorageSync("__WKIM__historyEmojis", this.historyEmojis);
  },
  // 获取最近使用表情
  getHistoryEmojis() {
    return this.historyEmojis;
  },
};

export default store;
